/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.lis.ctrl;

import com.cxhd.lis.model.newmodel.ZkMajorTest;
import static com.cxhd.lis.model.newmodel.ZkMajorTest.zkMajorTestDao;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import java.util.List;
import java.util.Map;

/**
 *
 * @author cxhd
 */
@ControllerBind(controllerKey ="view/lis/zkmt")
public class ZkMajorTestCtrl extends Controller {
     Gson  gson=new Gson();
     public void index(){
        render("/view/lis/zk_major_test.jsp");
    }
     
      public void getZkMajors(){
          String sql=" SELECT PK,MAJORNAME ";
          String param=" from  zk_major ";
       Page<Record> userPage =Db.use("mysql").paginate(getParaToInt("currentPage",getParaToInt("currentPage")), getParaToInt("pageSize",getParaToInt("pageSize")), sql, param);
       renderJson(userPage);
     }
     
         public void getZkMajorTests(){
        renderJson(Db.use("mysql").find("select PK,TESTPK,MAJORPK,(SELECT MAJORNAME FROM zk_major m WHERE mt.MAJORPK=m.PK ) MAJORNAME1,\n" +
"(SELECT TESTID FROM lis_testdescribe t where t.PK=mt.TESTPK ) TESTID1,(select CHINESENAME from lis_testdescribe t2 where t2.PK=mt.TESTPK ) CHINESENAME1  from zk_major_test mt where MAJORPK=?",getPara("PK")));
    }
    /**
     * 保存专业组与项目的匹配
     */
    @Before(Tx.class)
    public void saveZkMajorTests(){
        List<Map<String, String>> tests = gson.fromJson(getPara("TESTS"), new TypeToken<List<Map<String, String>>>() {}.getType());

        for(Map<String,String> tmp : tests){
     
            List<Record> list=Db.use("mysql").find("select * from zk_major_test where MAJORPK=? AND TESTPK=?",getPara("PK"),tmp.get("TESTPK"));
            if(list.size()==0){
                new ZkMajorTest().saveMajorTest(tmp.get("TESTPK"), getPara("PK"));

            }
            
        }
        renderText("true");
    }

    @Before(Tx.class)
    public void deleteMajorTest(){
        zkMajorTestDao.deleteMajotTest(getPara("MajorTestPK"));
       renderText("true");
    }

}
